﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Highchart.Core;
using System.ComponentModel;
using Highchart.Core.PlotOptions;
using Highchart.Core.Appearance;
using System.Collections.ObjectModel;

namespace Highchart.UI
{
    public abstract class GenericChart : CompositeDataBoundControl
    {
        /// <summary>
        /// An array containing the default colors for the chart's series.
        /// </summary>
        public virtual ColorSet Colors
        {
            get
            {  
                if (ViewState["Colors"] == null)
                {
                    ViewState["Colors"] = new ColorSet();
                }
                return (ColorSet)ViewState["Colors"];
            }
            set { ViewState["Colors"] = value; }
        }

        public virtual Localization Lang
        {
            get
            {
                if (ViewState["Lang"] == null)
                {
                    ViewState["Lang"] = new Localization();
                }
                return (Localization)ViewState["Lang"];
            }
            set { ViewState["Lang"] = value; }
        }

        /// <summary>
        /// By default, you can set: "skies", "grid", "gray", "dark-blue", "pink-floral" or "dark-green";
        /// </summary>
        public virtual string Theme
        {
            get
            {
                object o = ViewState["Theme"];
                if (o == null)
                    return string.Empty;
                return (string)o;
            }
            set { ViewState["Theme"] = value; }
        }

        public virtual Appearance Appearance
        {
            get
            {                
                if (ViewState["Appearance"] == null)
                {
                     ViewState["Appearance"] = new Appearance();                    
                }
                return (Appearance)ViewState["Appearance"];
            }
            set { ViewState["Appearance"] = value; }
        }

        public virtual Legend Legend
        {
            get
            {
                object o = ViewState["Legend"];
                if (o == null)
                {
                    var legend = new Legend();
                    ViewState["Legend"] = legend;
                    return legend;
                }
                return (Legend)o;
            }
            set { ViewState["Legend"] = value; }
        }

        public virtual Exporting Exporting
        {
            get
            {
                object o = ViewState["Exporting"];
                if (o == null)
                {
                    var export = new Exporting();
                    ViewState["Exporting"] = export;
                    return export;
                }
                return (Exporting)o;
            }
            set { ViewState["Exporting"] = value; }
        }

        public virtual ToolTip Tooltip
        {
            get
            {
                if (ViewState["ToolTipV"] == null)                    
                {
                    ViewState["ToolTipV"] = new ToolTip("'<b>'+ this.series.name + ((typeof(this.point.name) != 'undefined') ? '->'  +this.point.name : '' )+ ' </b><br/>'+ this.x +': '+ this.y");
                }
                return (ToolTip)ViewState["ToolTipV"];
            }
            set { ViewState["ToolTipV"] = value; }
        }

        public virtual YAxis YAxis
        {
            get
            {
                if (ViewState["YAxis"] == null)                
                 ViewState["YAxis"] = new YAxis();
                
                return (YAxis)ViewState["YAxis"];
            }
            set { ViewState["YAxis"] = value; }
        }

        public virtual XAxis XAxis
        {
            get
            {
                if (ViewState["XAxis"] == null)
                     ViewState["XAxis"] = new XAxis();

                return (XAxis)ViewState["XAxis"];
            }
            set { ViewState["XAxis"] = value; }
        }

        public virtual AJAXSource AjaxDataSource
        {
            get
            {
                if (ViewState["AJAXSource"] == null)
                    ViewState["AJAXSource"] = new AJAXSource();
                return (AJAXSource)ViewState["AJAXSource"];
            }
            set { ViewState["AJAXSource"] = value; }
        }


        /// <summary>
        /// Whether to show the credits text. Defaults to false.
        /// </summary>
        [Category("Appearance")]
        [DefaultValue(false)]
        public virtual bool ShowCredits
        {
            get
            {
                object o = ViewState["ShowCredits"];
                if (o == null)
                    return false;
                return (bool)o;
            }

            set
            {
                ViewState["ShowCredits"] = value;
            }
        }

        [DefaultValue("")]
        [Localizable(true)]
        public virtual Title Title
        {
            get
            {
                if (ViewState["Title"] == null)
                    ViewState["Title"] = new Title(string.Empty);
                return (Title)ViewState["Title"];
            }
            set { ViewState["Title"] = value; }
        }

        [DefaultValue("")]
        [Localizable(true)]
        public virtual SubTitle SubTitle
        {
            get
            {
                if (ViewState["SubTitle"] == null)
                    ViewState["SubTitle"] = new SubTitle(string.Empty);
                return (SubTitle)ViewState["SubTitle"];
            }
            set { ViewState["SubTitle"] = value; }
        }
        
        [DefaultValue(RenderType.column)]
        public virtual RenderType RenderType
        {
            get
            {
                object o = ViewState["RenderType"];
                if (o == null)
                    return RenderType.column;
                return (RenderType)o;
            }
            set { ViewState["RenderType"] = value; }
        }

        private SerieCollection _series;
        public virtual SerieCollection Series
        {
            get
            {
                _series = (SerieCollection)ViewState["SeriesCollection"];
                if (_series == null)
                {
                    _series = new SerieCollection();
                    if (IsTrackingViewState)
                    {
                        _series.TrackViewState();
                        ViewState["SeriesCollection"] = _series;
                    }
                    return _series;
                }

                if (IsTrackingViewState)
                    ViewState["SeriesCollection"] = _series;

                return _series;
            }
        }

        protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
        {
            return Series.Count;
        }
    }
}
